﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class FixedAngleLimitJoint : Joint
    {
        protected FarseerGames.FarseerPhysics.Dynamics.Joints.FixedAngleLimitJoint mFixedAngleLimitJoint;

        /// <summary>
        /// Gets or sets the minimum angle in degrees of the RigidBody.
        /// </summary>
        public Single LowerLimit { get { return MathHelper.ToDegrees(mFixedAngleLimitJoint.LowerLimit); } set { mFixedAngleLimitJoint.LowerLimit = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the minimum angle in radians of the RigidBody.
        /// </summary>
        public Single LowerLimitRadians { get { return mFixedAngleLimitJoint.LowerLimit; } set { mFixedAngleLimitJoint.LowerLimit = value; } }

        /// <summary>
        /// Gets or sets the maximum angle in degrees of the RigidBody.
        /// </summary>
        public Single UpperLimit { get { return MathHelper.ToDegrees(mFixedAngleLimitJoint.UpperLimit); } set { mFixedAngleLimitJoint.UpperLimit = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the maximum angle in radians of the RigidBody.
        /// </summary>
        public Single UpperLimitRadians { get { return mFixedAngleLimitJoint.UpperLimit; } set { mFixedAngleLimitJoint.UpperLimit = value; } }

        /// <summary>
        /// Gets or sets the slop allowed in the angle past the Min/Max limits.
        /// </summary>
        public Single Slop { get { return mFixedAngleLimitJoint.Slop; } set { mFixedAngleLimitJoint.Slop = value; } }

        /// <summary>
        /// Creates an instance of a FixedAngleLimitJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public FixedAngleLimitJoint(GameObject gameObject)
            : base(gameObject)
        {
            mFixedAngleLimitJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.FixedAngleLimitJoint();
            mJoint = mFixedAngleLimitJoint;
        }

        /// <summary>
        /// Create the Joint after setting initial settings.
        /// </summary>
        public override void Create()
        {
            mFixedAngleLimitJoint = JointFactory.Instance.CreateFixedAngleLimitJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, LowerLimitRadians, UpperLimitRadians);
            mJoint = mFixedAngleLimitJoint;
        }
    }
}
